package com.ladlee.validata;

import com.ladlee.validata.exception.ValidateCodeException;
import com.ladlee.validata.processor.ValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Auther: dingchang
 * @Date: 2018/12/21 19:08
 * @Description:
 */
@Component
public class ValidateCodeProcessorHolder {

    @Autowired
    private Map<String, ValidateCodeProcessor> validateCodeProcessors;

    public ValidateCodeProcessor findValidateCodeProcessor(ValidataCodeType type){
        return findValidateCodeProcessor(type.toString().toLowerCase());
    }

    public ValidateCodeProcessor findValidateCodeProcessor(String type){
        String typeName = type + ValidateCodeProcessor.class.getSimpleName();
        ValidateCodeProcessor codeProcessor = validateCodeProcessors.get(typeName);
        if(codeProcessor == null){
            throw new ValidateCodeException("验证码处理器" + typeName + "不存在");
        }
        return codeProcessor;
    }
}
